//
//  UIImage+waterMark.m
//  watermark_Demo
//
//  Created by GC_tandy on 2018/6/22.
//  Copyright © 2018年 GC_tandy. All rights reserved.
//

#import "UIImage+waterMark.h"
#define waterMark_ratio  0.3//水印宽度相对于目标图片占比
#define waterMark_edge   0.05//水印边距相对于目标图片占比

@implementation UIImage (waterMark)

- (UIImage *)addWaterMarkWithImage:(UIImage *)waterMarkImage{
    
    if (self == nil) return nil;
    if (waterMarkImage == nil) return self;
    

    // 开始上下文，下面不使用时一定要关闭，从上下文栈中移除
    UIGraphicsBeginImageContextWithOptions(self.size, NO,self.scale);

    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

    
    // 在圆区内画出image原图
    [self drawInRect:rect];
    CGFloat width_w = self.size.width*waterMark_ratio;
    CGFloat height_w = width_w*waterMarkImage.size.height/waterMarkImage.size.width;
    
    CGFloat x_w = self.size.width*(1-waterMark_ratio-waterMark_edge);
    CGFloat y_w = self.size.height*(1-waterMark_edge)-height_w;
    
    CGRect rect_w = CGRectMake(x_w, y_w, width_w, height_w);
    [waterMarkImage drawInRect:rect_w];
    

    //从上下文环境中获取切好的图片
    UIImage *resultImage  = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使用了beginImgacontext需要关闭上下文 并从上下文栈中移除
    UIGraphicsEndImageContext();
    

    return resultImage;
}

@end
